<template>
  <div class="m-content">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="5%" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title" placeholder="请输入标题..." ref="inputs"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <mavon-editor v-model="ruleForm.content" placeholder="请输入内容..." style="z-index:1;border: 1px solid #d9d9d9;height:74vh"></mavon-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="submitForm('ruleForm')">{{btnInfo}}</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {postNote} from "../../network/Details/note/note";
import {request} from "../../network/request";
export default {
  name: "NoteMark",
  data() {
    return {
      ruleForm: {
        title: '',
        content: '',
        noteId: 0,
        username: window.sessionStorage.getItem("username")
      },
      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {min: 3, max: 100, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入内容', trigger: 'blur'}
        ],

      },
      btnInfo: '添加笔记'
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let method
          if(this.$route.query.noteId == null){
                       method = 'post'
           }else {
                       method = 'put'

           }
          postNote(this.ruleForm,method).then(res => {
            let noteId = res.data
            if(this.$route.query.noteId != null){
                noteId = this.$route.query.noteId
              }
              this.$alert('保存成功', '提示', {
                confirmButtonText: '确定',
                callback: action => {
                   this.$router.replace({
                    name:"NoteDetail",
                    query:{
                      noteId: noteId
                    }
                  })
                }
              })
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    autoSave(){

    }
  },
  created() {
    const noteId = this.$route.query.noteId;
    if(noteId != null){
      this.btnInfo = "更新笔记"
      this.ruleForm.noteId = noteId;
      request({
        url: '/note/' + this.$route.query.noteId,
        method: 'get'
      }).then(res => {
        let note =  res.data
        this.ruleForm.title = note.title
        this.ruleForm.content = note.content
      })
    }
    this.autoSave();
  },
  mounted(){
    this.$nextTick(() => {
      this.$refs.inputs.focus() //通过 $refs 获取dom 并绑定 focus 方法
    })
  }
}
</script>

<style scoped>
.m-content {
  width: 98%;
  margin-top: 2%;
}

</style>
